﻿Imports TransportadoraToledoBLL
Public Class frm_consultas

    Public tipo As String
    Public numero As Integer
    Public codigo As Integer
    Public protocolo As String

    Sub New(ByVal tp As String)
        InitializeComponent()
        tipo = tp
    End Sub

    Sub New(ByVal tp As String, ByVal num As Integer)

        ' This call is required by the designer.
        InitializeComponent()
        tipo = tp
        numero = num
        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Sub atualizaGrid()
        Select Case (tipo)
            Case "tipo_cargas"
                Try
                    Me.Text = "Consulta dos Tipos da Carga"
                    Dim objBll As New tiposCargasBLL
                    lDescricao.Text = "Digite o tipo da carga"
                    dgConsulta.DataSource = objBll.localizar(tbPesquisar.Text)
                    dgConsulta.Columns(0).HeaderText = "Código"
                    dgConsulta.Columns(1).HeaderText = "Descriçao"
                    dgConsulta.Columns(2).HeaderText = "Ativo"

                    dgConsulta.Columns(0).Width = 45
                    dgConsulta.Columns(1).Width = 150
                    dgConsulta.Columns(2).Width = 100
                Catch ex As Exception
                    Throw ex
                End Try
            Case "cargas"
                Try
                    Me.Text = "Consulta de Cargas"
                    Dim objBll As New CargasBLL
                    lDescricao.Text = "Digite o Protocolo da Carga"
                    dgConsulta.DataSource = objBll.localizarProtocolo(tbPesquisar.Text)
                    dgConsulta.Columns(0).HeaderText = "Código"
                    dgConsulta.Columns(1).HeaderText = "Protocolo"
                    dgConsulta.Columns(2).Visible = False
                    dgConsulta.Columns(3).Visible = False
                    dgConsulta.Columns(4).HeaderText = "Valor da Carga"
                    dgConsulta.Columns(5).Visible = False
                    dgConsulta.Columns(0).Width = 100
                    dgConsulta.Columns(1).Width = 200
                    dgConsulta.Columns(4).Width = 200
                Catch ex As Exception
                    Throw ex
                End Try
            Case "tipo_cargas"
                Try
                    Me.Text = "Consulta dos Tipos da Carga"
                    Dim objBll As New tiposCargasBLL
                    lDescricao.Text = "Digite o tipo da carga"
                    dgConsulta.DataSource = objBll.localizar(tbPesquisar.Text)
                    dgConsulta.Columns(0).HeaderText = "Código"
                    dgConsulta.Columns(1).HeaderText = "Descriçao"
                    dgConsulta.Columns(2).HeaderText = "Ativo"

                    dgConsulta.Columns(0).Width = 45
                    dgConsulta.Columns(1).Width = 150
                    dgConsulta.Columns(2).Width = 100
                Catch ex As Exception
                    Throw ex
                End Try
            Case "lotação"
                Try
                    Me.Text = "Consulta de Lotação"
                    Dim objBll As New EntregasBLL
                    lDescricao.Text = "Informe o período de Data da lotação"
                    tbPesquisar.Visible = False
                    lblDatainicio.Visible = True
                    lblDtfim.Visible = True
                    dtFinal.Visible = True
                    dtInicio.Visible = True
                    dgConsulta.DataSource = objBll.localizarLotacao(dtInicio.Text, dtFinal.Text)
                    dgConsulta.Columns(0).HeaderText = "Código Lotação"
                    dgConsulta.Columns(1).Visible = False
                    dgConsulta.Columns(2).HeaderText = "Data Lotação"
                    dgConsulta.Columns(4).HeaderText = "Status Lotação"
                    dgConsulta.Columns(0).Width = 200
                    dgConsulta.Columns(2).Width = 300
                    dgConsulta.Columns(3).Visible = False
                    dgConsulta.Columns(4).Width = 280

                    dgConsulta.RowHeadersVisible = False
                    dgConsulta.AllowUserToAddRows = False
                    dgConsulta.ReadOnly = True
                Catch ex As Exception
                    Throw ex
                End Try
            Case "entregas"
                Try
                    Me.Text = "Consulta de Entregas"
                    Dim objBll As New EntregasBLL
                    lDescricao.Text = "Informe o período de Data do Retorno de Entrega"
                    tbPesquisar.Visible = False
                    lblDatainicio.Visible = True
                    lblDtfim.Visible = True
                    dtFinal.Visible = True
                    dtInicio.Visible = True
                    dgConsulta.DataSource = objBll.localizarEntrega(dtInicio.Text, dtFinal.Text)
                    dgConsulta.Columns(0).HeaderText = "Código Entrega"
                    dgConsulta.Columns(1).HeaderText = "Data Retorno"
                    dgConsulta.Columns(2).HeaderText = "Código Lotação"
                    dgConsulta.Columns(3).HeaderText = "Status Entrega"
                    dgConsulta.Columns(0).Width = 100
                    dgConsulta.Columns(1).Width = 300
                    dgConsulta.Columns(2).Width = 100
                    dgConsulta.Columns(3).Width = 280

                    dgConsulta.RowHeadersVisible = False
                    dgConsulta.AllowUserToAddRows = False
                    dgConsulta.ReadOnly = True
                Catch ex As Exception
                    Throw ex
                End Try
            Case "Seguros"
                    Try
                        Dim objBll As New SeguroBLL
                        lDescricao.Text = "Digite o tipo da carga"
                        dgConsulta.DataSource = objBll.localizar(tbPesquisar.Text)
                        dgConsulta.Columns(0).HeaderText = "Código"
                        dgConsulta.Columns(1).HeaderText = "Nome"
                        dgConsulta.Columns(2).HeaderText = "Descrição"
                        dgConsulta.Columns(3).HeaderText = "Valor"
                        dgConsulta.Columns(4).HeaderText = "Ativo"

                        dgConsulta.Columns(0).Width = 45
                        dgConsulta.Columns(1).Width = 150
                        dgConsulta.Columns(2).Width = 100
                        dgConsulta.Columns(3).Width = 50
                        dgConsulta.Columns(4).Width = 50
                    Catch ex As Exception
                        Throw ex
                    End Try

            Case "historico_cargas"
                Try
                    Me.Text = "Consulta Histórico de Cargas"
                    Dim objBll As New historicoCargasBLL
                    lDescricao.Text = "Digite o Protocolo da Carga"
                    dgConsulta.DataSource = objBll.localizar(tbPesquisar.Text)
                    dgConsulta.Columns(0).HeaderText = "Código"
                    dgConsulta.Columns(1).HeaderText = "Carga"
                    dgConsulta.Columns(2).HeaderText = "Data"
                    dgConsulta.Columns(3).HeaderText = "Valor"

                    dgConsulta.Columns(0).Width = 45
                    dgConsulta.Columns(1).Width = 150
                    dgConsulta.Columns(2).Width = 100
                    dgConsulta.Columns(3).Width = 50
                Catch ex As Exception
                    Throw
                End Try
            Case "Veiculos"
                Try
                    Dim objBll As New VeiculosBLL
                    lDescricao.Text = "Digite a placa do veículo"
                    dgConsulta.DataSource = objBll.localizar(tbPesquisar.Text)
                    dgConsulta.Columns(0).HeaderText = "Código"
                    dgConsulta.Columns(1).HeaderText = "Modelo"
                    dgConsulta.Columns(3).HeaderText = "Placa"
                    dgConsulta.Columns(2).Visible = False
                    dgConsulta.Columns(4).Visible = False
                    dgConsulta.Columns(5).Visible = False
                    dgConsulta.Columns(6).Visible = False
                    dgConsulta.Columns(7).Visible = False
                    dgConsulta.Columns(8).Visible = False
                    dgConsulta.Columns(9).Visible = False
                    dgConsulta.Columns(10).Visible = False
                    dgConsulta.Columns(11).Visible = False


                    dgConsulta.Columns(0).Width = 45
                    dgConsulta.Columns(1).Width = 150
                    dgConsulta.Columns(3).Width = 50
                Catch ex As Exception
                    Throw ex
                End Try

            Case "Clientes"
                Try
                    Me.Text = "Consulta de Clientes"
                    lDescricao.Text = "Nome do Cliente"
                    Dim objBll As New ClienteBLL
                    lDescricao.Text = "Digite o Nome do Cliente"
                    dgConsulta.DataSource = objBll.localizar(tbPesquisar.Text)
                    dgConsulta.Columns(0).HeaderText = "Código"
                    dgConsulta.Columns(1).HeaderText = "Nome"
                    dgConsulta.Columns(2).HeaderText = "Logradouro"
                    dgConsulta.Columns(3).HeaderText = "Complemento"
                    dgConsulta.Columns(4).HeaderText = "Numero"
                    dgConsulta.Columns(5).HeaderText = "Bairro"
                    dgConsulta.Columns(6).HeaderText = "CEP"
                    dgConsulta.Columns(7).HeaderText = "Logradouro da Cobrança"
                    dgConsulta.Columns(8).HeaderText = "Complemento da Cobrança"
                    dgConsulta.Columns(9).HeaderText = "Número da Cobrança"
                    dgConsulta.Columns(10).HeaderText = "Bairro para Cobrança"
                    dgConsulta.Columns(11).HeaderText = "CEP para Cobrança"
                    dgConsulta.Columns(12).HeaderText = "Telefone Fixo"
                    dgConsulta.Columns(13).HeaderText = "Celular"
                    dgConsulta.Columns(14).HeaderText = "Telefone para Recado"
                    dgConsulta.Columns(15).HeaderText = "Tipo de Pessoa (F/J)"
                Catch ex As Exception
                    Throw
                End Try
            Case "ordem_servico"
                Try
                    Me.Text = "Consultar Veículo"
                    Dim objBll As New VeiculosBLL
                    lDescricao.Text = "Digite a placa do veículo"
                    dgConsulta.DataSource = objBll.veiculosdisponiveis(tbPesquisar.Text)
                    dgConsulta.Columns(0).HeaderText = "Código"
                    dgConsulta.Columns(3).HeaderText = "Placa"

                    dgConsulta.Columns(1).Visible = False
                    dgConsulta.Columns(2).Visible = False
                    dgConsulta.Columns(4).Visible = False
                    dgConsulta.Columns(5).Visible = False
                    dgConsulta.Columns(6).Visible = False
                    dgConsulta.Columns(7).Visible = False
                    dgConsulta.Columns(8).Visible = False
                    dgConsulta.Columns(9).Visible = False
                    dgConsulta.Columns(10).Visible = False
                    dgConsulta.Columns(11).Visible = False
                Catch ex As Exception
                    Throw ex
                End Try
            Case "marcas"
                Try
                    Me.Text = "Consultar Marcas"
                    Dim objBll As New MarcasBLL
                    lDescricao.Text = "Digite o nome da marca"
                    dgConsulta.DataSource = objBll.localizar(tbPesquisar.Text)
                    dgConsulta.Columns(0).HeaderText = "Código"
                    dgConsulta.Columns(1).HeaderText = "Nome"

                Catch ex As Exception
                    Throw ex
                End Try
            Case "modelos"
                Try
                    Me.Text = "Consultar Modelos"
                    Dim objBll As New ModelosBLL
                    lDescricao.Text = "Digite o nome do modelo"
                    dgConsulta.DataSource = objBll.localizar(tbPesquisar.Text, numero)
                    dgConsulta.Columns(0).HeaderText = "Código"
                    dgConsulta.Columns(1).HeaderText = "Marca"
                    dgConsulta.Columns(2).HeaderText = "Modelo"
                    dgConsulta.Columns(2).Width = 250

                Catch ex As Exception
                    Throw ex
                End Try

            Case "combustivel"
                Try
                    Me.Text = "Consultar Combustíveis"
                    Dim objBll As New CombustivelBLL
                    lDescricao.Text = "Digite o tipo do combustível"
                    dgConsulta.DataSource = objBll.localizar(tbPesquisar.Text)
                    dgConsulta.Columns(0).HeaderText = "Código"
                    dgConsulta.Columns(1).HeaderText = "Tipo"

                Catch ex As Exception
                    Throw ex
                End Try

            Case "veiculos_disponiveis"
                Try
                    Me.Text = "Consultar veiculos disponiveis para agendamento"
                    lDescricao.Visible = False
                    tbPesquisar.Visible = False
                    dtInicio.Visible = True
                    dtInicio.Size = New Point(143.2)
                    lblDatainicio.Visible = True
                    lblDatainicio.Size = New Point(63.13)
                    lblDatainicio.Text = "Data para agendar:"

                    Dim objBll As New VeiculosBLL
                    dgConsulta.DataSource = objBll.veiculosdisponiveis(dtInicio.Value.Date)
                    dgConsulta.Columns(0).HeaderText = "Código"
                    dgConsulta.Columns(1).HeaderText = "Código Modelo"
                    'dgConsulta.Columns(2).HeaderText = "Código Combustivel"
                    dgConsulta.Columns(2).HeaderText = "Placa Veiculo"
                    dgConsulta.Columns(3).HeaderText = "Cor" '
                    dgConsulta.Columns(4).HeaderText = "Categoria"
                    dgConsulta.Columns(5).HeaderText = "N° Chassi" '
                    dgConsulta.Columns(6).HeaderText = "Capacidade"
                    dgConsulta.Columns(7).HeaderText = "Ano" '
                    dgConsulta.Columns(8).HeaderText = "Modelo" '
                    dgConsulta.Columns(9).HeaderText = "Ativo S/N"
                    dgConsulta.Columns(10).HeaderText = "Data Cadastro"
                    dgConsulta.Columns(1).Visible = False
                    dgConsulta.Columns(3).Visible = False
                    dgConsulta.Columns(5).Visible = False
                    dgConsulta.Columns(6).Visible = False
                    dgConsulta.Columns(8).Visible = False
                Catch ex As Exception
                    Throw ex
                End Try

            Case "pessoa"
                Try
                    Me.Text = "Consultar Pessoas"
                    Dim objBll As New UsuariosBLL
                    lDescricao.Text = "Digite o nome da pessoa"
                    dgConsulta.DataSource = objBll.localizarPessoa(tbPesquisar.Text)
                    dgConsulta.Columns(0).HeaderText = "Código"
                    dgConsulta.Columns(1).HeaderText = "Nome"
                    For Each coluna As DataGridViewColumn In dgConsulta.Columns
                        If coluna.Index = 0 Then
                            coluna.HeaderText = "Código"
                        ElseIf coluna.Index = 1 Then
                            coluna.HeaderText = "Nome"
                        Else
                            coluna.Visible = False
                        End If
                    Next
                Catch ex As Exception
                    Throw ex
                End Try

            Case "cargas_disponiveis"
                Try
                    Me.Text = "Consultar cargas disponiveis para lotação"
                    Dim objBll As New LotacaoBLL
                    lDescricao.Text = "Digite o Protocolo da Carga"
                    dgConsulta.DataSource = objBll.localizarCargasDisponiveis(tbPesquisar.Text)
                    dgConsulta.Columns(0).HeaderText = "Código"
                    dgConsulta.Columns(1).HeaderText = "Protocolo"
                    dgConsulta.Columns(2).HeaderText = "Peso"
                    dgConsulta.Columns(3).HeaderText = "Agendamento"
                    dgConsulta.Columns(4).HeaderText = "Remetente"
                    dgConsulta.Columns(5).HeaderText = "Destinatario"
                    dgConsulta.Columns(6).HeaderText = "Data do Agendamento"
                    dgConsulta.Columns(7).HeaderText = "Previsão de Entrega"
                    dgConsulta.Columns(8).HeaderText = "Rota"
                    dgConsulta.Columns(9).HeaderText = "Distância"
                Catch ex As Exception
                    Throw ex
                End Try

        End Select

    End Sub

    Private Sub bPesquisar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPesquisar.Click
        atualizaGrid()
    End Sub

    Private Sub dgDados_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgConsulta.CellDoubleClick

        Try
            Select Case tipo
                Case "tipo_cargas"
                    If (e.RowIndex >= 0) Then
                        codigo = dgConsulta.Rows(e.RowIndex).Cells("tc_codigo").Value
                        Me.Close()
                    End If
                Case "cargas"
                    If (e.RowIndex >= 0) Then
                        codigo = dgConsulta.Rows(e.RowIndex).Cells("car_codigo").Value
                        protocolo = dgConsulta.Rows(e.RowIndex).Cells("car_protocolo").Value
                        Me.Close()
                    End If
                Case "lotação"
                    If (e.RowIndex >= 0) Then
                        codigo = dgConsulta.Rows(e.RowIndex).Cells("lot_codigo").Value
                        Me.Close()
                    End If
                Case "entregas"
                    If (e.RowIndex >= 0) Then
                        codigo = dgConsulta.Rows(e.RowIndex).Cells("ent_codigo").Value
                        Me.Close()
                    End If
                Case "Seguros"
                    If (e.RowIndex >= 0) Then
                        codigo = dgConsulta.Rows(e.RowIndex).Cells("seg_codigo").Value
                        Me.Close()
                    End If
                Case "historico_cargas"
                    If (e.RowIndex >= 0) Then
                        codigo = dgConsulta.Rows(e.RowIndex).Cells("hc_codigo").Value
                        Me.Close()
                    End If
                Case "Clientes"
                    If (e.RowIndex >= 0) Then
                        codigo = dgConsulta.Rows(e.RowIndex).Cells("pes_codigo").Value
                        Me.Close()
                    End If
                Case "ordem_servico"
                    If (e.RowIndex >= 0) Then
                        codigo = dgConsulta.Rows(e.RowIndex).Cells("vei_codigo").Value
                        Me.Close()
                    End If
                Case "marcas"
                    If (e.RowIndex >= 0) Then
                        codigo = dgConsulta.Rows(e.RowIndex).Cells("mar_codigo").Value
                        Me.Close()
                    End If
                Case "modelos"
                    If (e.RowIndex >= 0) Then
                        codigo = dgConsulta.Rows(e.RowIndex).Cells("mod_codigo").Value
                        Me.Close()
                    End If
                Case "combustivel"
                    If (e.RowIndex >= 0) Then
                        codigo = dgConsulta.Rows(e.RowIndex).Cells("com_codigo").Value
                        Me.Close()
                    End If
                Case "Veiculos"
                    If (e.RowIndex >= 0) Then
                        codigo = dgConsulta.Rows(e.RowIndex).Cells("vei_codigo").Value
                        Me.Close()
                    End If

                Case "veiculos_disponiveis"
                    If (e.RowIndex >= 0) Then
                        codigo = dgConsulta.Rows(e.RowIndex).Cells("vei_codigo").Value
                        Me.Close()
                    End If

                Case "pessoa"
                    If (e.RowIndex >= 0) Then
                        codigo = dgConsulta.Rows(e.RowIndex).Cells("pes_codigo").Value
                        tipo = dgConsulta.Rows(e.RowIndex).Cells("pes_nome").Value
                        Me.Close()
                    End If

                Case "cargas_disponiveis"
                    If (e.RowIndex >= 0) Then
                        codigo = dgConsulta.Rows(e.RowIndex).Cells("car_codigo").Value
                        Me.Close()
                    End If
            End Select
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Erro")
        End Try

    End Sub

    Private Sub frm_consultas_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        atualizaGrid()
    End Sub

    Private Sub tbPesquisar_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbPesquisar.TextChanged
        atualizaGrid()
    End Sub

    Private Sub dtFinal_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtFinal.Leave
        If (Date.Parse(dtFinal.Text) < Date.Parse(dtInicio.Text)) Then
            MsgBox("Data de Final deve ser maior ou igual a Data de Inicio!")
            dtFinal.Text = Date.Now
            dtFinal.Focus()
            Exit Sub
        End If
    End Sub

    Private Sub dtInicio_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtInicio.Leave
        If (tipo <> "veiculos_disponiveis") Then
            If (Date.Parse(dtFinal.Text) < Date.Parse(dtInicio.Text)) Then
                MsgBox("Data de Inicio deve ser menor ou igual a Data de Final!")
                dtInicio.Text = Date.Now
                dtInicio.Focus()
                Exit Sub
            End If
        End If
    End Sub

    Private Sub dtFinal_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtFinal.ValueChanged
        If (Date.Parse(dtFinal.Text) < Date.Parse(dtInicio.Text)) Then
            MsgBox("Data de Final deve ser maior ou igual a Data de Inicio!")
            dtFinal.Text = Date.Now
            dtFinal.Focus()
            Exit Sub
        End If
    End Sub

    Private Sub dtInicio_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtInicio.ValueChanged
        If (tipo <> "veiculos_disponiveis") Then
            If (Date.Parse(dtFinal.Text) < Date.Parse(dtInicio.Text)) Then
                MsgBox("Data de Inicio deve ser menor ou igual a Data de Final!")
                dtInicio.Text = Date.Now
                dtInicio.Focus()
                Exit Sub
            End If
        End If
        If (tipo = "veiculos_disponiveis") Then
            atualizaGrid()
        End If
    End Sub
End Class